package com.pactera.demo.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.demo.base.dto.UserBindDto;
import com.pactera.demo.base.dto.UserDto;
import com.pactera.demo.base.dto.UserRole;
import com.pactera.demo.base.entity.LoginUser;
import com.pactera.demo.base.entity.User;
import com.pactera.demo.base.service.IUserService;
import com.pactera.demo.common.dto.ResponseResult;
import com.pactera.demo.common.enums.StatusCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户个人信息表 前端控制器
 * </p>
 *
 * @author mybatis.plus
 * @since 2020-02-27
 */
@RestController
@RequestMapping("/base")
public class UserController {

    private IUserService userService;

    @PostMapping("/user")
    public ResponseResult createUser(@RequestBody @Valid UserDto userDto, BindingResult result){
        if(result.hasErrors()){
            List<String> errors = result.getAllErrors().stream()
                    .map(DefaultMessageSourceResolvable::getDefaultMessage)
                    .collect(Collectors.toList());
            return ResponseResult.fail(StatusCodeEnum.ILLEGAL_PARAM, errors);
        }

        User user = new User(userDto.getName(),userDto.getIdNum(),userDto.getDeptId());
        user.setUserType(0);
        user.setEnabled(0);

        LoginUser loginUser = new LoginUser(userDto.getLoginName(),"{noop}12345");

        this.userService.saveUser(user,loginUser);
        return ResponseResult.success();
    }

    @GetMapping("/user/{pageSize}/{index}")
    public ResponseResult list(@PathVariable("pageSize") int pageSize, @PathVariable("index") int index){
        IPage<User> page = this.userService.page(new Page<>(index, pageSize),
                new QueryWrapper<User>().lambda().eq(User::getEnabled, 0));
        return ResponseResult.success(page);
    }

    @PostMapping("/user/bind")
    public ResponseResult bind(@RequestBody @Valid UserBindDto userBindDto,BindingResult result){
        if(result.hasErrors()){
            List<String> errors = result.getAllErrors().stream()
                    .map(DefaultMessageSourceResolvable::getDefaultMessage)
                    .collect(Collectors.toList());
            return ResponseResult.fail(StatusCodeEnum.ILLEGAL_PARAM, errors);
        }

        List<UserRole> userRoles = userBindDto.getRoleIds().stream().map(x -> new UserRole(userBindDto.getUserId(), x)).collect(Collectors.toList());
        this.userService.bindRolesToUser(userRoles);
        return ResponseResult.success();
    }

    public UserController(IUserService userService) {
        this.userService = userService;
    }



}

